package com.ebelter.bodyfatscale.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;



public class LineView extends View {
    //public static final float WUXIAO_VALUE = Float.MIN_VALUE;

    private int dp2px(Context context, float dipValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
    private int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }
    public LineView(Context context) {
        this(context,null);
    }
    public LineView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }




    Paint mPaint;
    int mWidth;
    int mHeight;
    int halfWidth;
    int lineHeight;
    int lineWeight;
    int textSize;

    int color1;
    int color2;
    int color3;

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿
        lineWeight= dp2px(getContext(),2);
        mPaint.setStrokeWidth(lineWeight);
        textSize=sp2px(getContext(),6);
        mPaint.setTextSize(textSize);
        textWidth1=getTextWidth(mPaint,"X");
        textWidth2=getTextWidth(mPaint,"XX");
        textWidth3=getTextWidth(mPaint,"XXX");
        textWidth4=getTextWidth(mPaint,"XXXX");
        textWidth5=getTextWidth(mPaint,"XXXXX");
        textWidth7=getTextWidth(mPaint,"2018-24");
        textWidth10=getTextWidth(mPaint,"2018-06-15");
        color1=Color.parseColor("#232e40");
        color2=Color.parseColor("#0158db");
        color3=Color.parseColor("#ffffff");
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // ULog.i(TAG,"onMeasure()---屏幕的宽："+ DipUtil.getWindowWidth(getContext())+"---屏幕的高："+DipUtil.getWindowHeight(getContext()));
        int width = getMySize(100, widthMeasureSpec);
        int height = getMySize(100, heightMeasureSpec);
        // ULog.i(TAG,"onMeasure()---控件的宽："+ width+"---控件的高："+height);
        setMeasuredDimension(width,height);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth=getMeasuredWidth();
        halfWidth=mWidth/2;
        mHeight=getMeasuredHeight();
        lineHeight= (int) (mHeight*0.9);//0.9是看UI图比例计算出来的
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        drawHachures(canvas);//阴影线

        drawSolidLine(canvas);//实线

        drawTitles(canvas);//横向标题栏

        drawValueTitle(canvas);//画数值标题栏
    }

    private void drawValueTitle(Canvas canvas) {
        if (TextUtils.isEmpty(valueTitle)){
            return;
        }
        mPaint.setColor(color3);// 白色
        float startX2=halfWidth-getWellSize(valueTitle)/2.0f;
        float stopY=lineHeight*(1-lineValuePercentage)*animationPercentage;
        canvas.drawText(valueTitle,startX2, (float) (stopY-(mPaint.getTextSize()/2.0)),mPaint);

    }


    private void drawTitles(Canvas canvas) {
        if (TextUtils.isEmpty(title)){
            return;
        }
        mPaint.setColor(color3);// 白色



        float startX2=halfWidth-getWellSize(title)/2.0f;
        canvas.drawText(title,startX2, (float) (mHeight-(mPaint.getTextSize()/2.0)),mPaint);

    }

    private float getWellSize(String title){
        float textSize1=sp2px(getContext(),6);
        mPaint.setTextSize(textSize1);
        int wight = getTextWidth(mPaint,title);
        if (wight<mWidth){
            return wight;
        }
        for (int i=0;i<60;i++){
            float textSize2=sp2px(getContext(),6-i*0.01f);
            mPaint.setTextSize(textSize2);
            wight = getTextWidth(mPaint,title);
            if (wight<mWidth){
                return wight;
            }
        }
        return wight;
    }

    public void setValues(float lineValuePercentage,String title,float animationPercentage){
        if (lineValuePercentage<0){
            lineValuePercentage=0;
        }
        this.lineValuePercentage=lineValuePercentage;
        this.title=title;
        this.animationPercentage=animationPercentage;
        invalidate();
    }

    public void setAnimationPercentage(float animationPercentage){
        this.animationPercentage=animationPercentage;
        invalidate();
    }

    private int getTextWidth(String string){
        if (TextUtils.isEmpty(string)){
            return 0;
        }
        switch (string.length()){
            case 1:
                return textWidth1;
            case 2:
                return textWidth2;
            case 3:
                return textWidth3;
            case 4:
                return textWidth4;
            case 5:
                return textWidth5;
            case 7:
                return textWidth7;
            case 10:
                return textWidth10;
            default:
                return textWidth1*string.length();
        }
    }

    public float lineValuePercentage;//当前线应该画的高度比例
    public float animationPercentage=1.0f;//动画百分比
    public String title = "";
    public String valueTitle = "";

    public void setValueTitle(String valueTitle) {
        this.valueTitle = valueTitle;
    }

    private void drawSolidLine(Canvas canvas) {
        mPaint.setColor(color2);//这样获取颜色ok的 阴影线颜色
        float stopY=lineHeight*(1-lineValuePercentage)*animationPercentage;
        canvas.drawLine(halfWidth,lineHeight,halfWidth,stopY,mPaint);//阴影线

    }


    private void drawHachures(Canvas canvas) {
        mPaint.setColor(color1);//这样获取颜色ok的 阴影线颜色
        canvas.drawLine(halfWidth,0,halfWidth,lineHeight,mPaint);//阴影线
    }

    private int getMySize(int defaultSize, int measureSpec) {
        int mySize = defaultSize;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);//这里的的尺寸大小并不是最终我们的View的尺寸大小，而是父View提供的参考大小
        switch (mode) {
            case MeasureSpec.UNSPECIFIED: {//如果没有指定大小，就设置为默认大小
                mySize = defaultSize;
                break;
            }
            case MeasureSpec.AT_MOST: {//如果测量模式是最大取值为size
                //我们将大小取最大值,你也可以取其他值
                mySize = size;
                break;
            }
            case MeasureSpec.EXACTLY: {//如果是固定的大小，那就不要去改变它
                mySize = size;
                break;
            }
        }
        return mySize;
    }


    int textWidth1;
    int textWidth2;
    int textWidth3;
    int textWidth4;
    int textWidth5;
    int textWidth7;
    int textWidth10;

    public int getTextWidth(Paint paint, String str) {
        int w= 0;
        if (str != null && str.length() > 0) {
            int len = str.length();
            float[] widths = new float[len];
            paint.getTextWidths(str, widths);
            for (int j = 0; j < len; j++) {
                w+= (int) Math.ceil(widths[j]);
            }
        }
        return w;
    }
}
